package com.dbn.webmagic.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dbn.webmagic.utils.AhttpRequest;
import com.dbn.webmagic.dao.NoticeDao;
import com.dbn.webmagic.entity.NoticeEntity;
import com.dbn.webmagic.entity.NoticeList;
import com.dbn.webmagic.service.NoticeService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service("noticeService")
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, NoticeEntity> implements NoticeService {
    @Autowired
    private NoticeDao noticeDao;

    @Override
    public int add(NoticeEntity noticeEntity) {
        int a=noticeDao.insert(noticeEntity);
        return a;
    }

    @Override
    public String noticesearch() {
        //定义查询url
        String url="http://www.nybkjfzzx.cn/p_pzbh/sub_ggxq.aspx?t=";
        //定义flag，0进行综合查询，1进行公告查询
        int flag=1;
        List<NoticeList> Noticelist= AhttpRequest.r(url,flag);
        for (NoticeList entity : Noticelist) {
            NoticeEntity noticeEntity=new NoticeEntity();
            String o=entity.getUrl();
            try {
                Document document= Jsoup.connect(o).get();
                Elements aa=document.getElementById("Content_tbInfo").getElementsByTag("tr");
                String noticetitle=document.getElementById("Content_lblTitle").text();//公告标题
                String phase=document.getElementById("Content_lblSubTitle").text();//公告期刊
                noticeEntity.setNoticetitle(noticetitle);
                noticeEntity.setPhase(phase);

                //循环tr文本 noticeEntity.setNoticesimpleid(entity.getNoticeSimpleId());
                for (Element element : aa) {
                    String s=element.getElementsByTag("td").get(0).text();
                    String leftname=s.replace("\u00A0", "");//去空格
                    String rightname=element.getElementsByTag("td").get(1).text();
                    if(leftname.equals("植物种类")){
                        noticeEntity.setPlanttype(rightname);
                    }if(leftname.equals("品种暂定名称")){
                        noticeEntity.setZstempname(rightname);
                    }if(leftname.equals("品种名称")){
                        noticeEntity.setTempname(rightname);
                    }if(leftname.equals("申请号")){
                        noticeEntity.setApplyno(rightname);
                    }if(leftname.equals("申请日")){
                        noticeEntity.setApplydate(rightname);
                    }if(leftname.equals("申请人")){
                        noticeEntity.setApplyname(rightname);
                    }if(leftname.equals("公告日")){
                        noticeEntity.setNodedate(rightname);
                    }if(leftname.equals("公告号")){
                        noticeEntity.setNodeno(rightname);
                    }if(leftname.equals("培育人")){
                        noticeEntity.setFostername(rightname);
                    }if(leftname.equals("申请人地址")){
                        noticeEntity.setApplymanadd(rightname);
                    }if(leftname.equals("申请日前销售情况")){
                        noticeEntity.setApplysell(rightname);
                    }if(leftname.equals("类型")){
                        noticeEntity.setNoticetype(rightname);
                    }if(leftname.equals("原因")){
                        noticeEntity.setReason(rightname);
                    }if(leftname.equals("品种权人")){
                        noticeEntity.setVariety(rightname);
                    }if(leftname.equals("品种权人地址")){
                        noticeEntity.setVarietyadd(rightname);
                    }if(leftname.equals("代理机构")){
                        noticeEntity.setAgency(rightname);
                    }if(leftname.equals("代理机构地址")){
                        noticeEntity.setAgencyadd(rightname);
                    }if(leftname.equals("代理人")){
                        noticeEntity.setAgencyman(rightname);
                    }if(leftname.equals("品种权证书号")){
                        noticeEntity.setVarietycartno(rightname);
                    }if(leftname.equals("变更后")){
                        noticeEntity.setAfterchange(rightname);
                    }if(leftname.equals("变更前")){
                        noticeEntity.setBeforchange(rightname);
                    }if(leftname.equals("转让前")){
                        noticeEntity.setBefortran(rightname);
                    }if(leftname.equals("转让后")){
                        noticeEntity.setAftertran(rightname);
                    }if(leftname.equals("终止日")){
                        noticeEntity.setStopdate(rightname);
                    }
                }
                QueryWrapper<NoticeEntity> queryWrapper=new QueryWrapper();
                queryWrapper.eq("noticesimpleid",entity.getNoticeSimpleId());
                int a=noticeDao.selectCount(queryWrapper);
                if(a==0){
                    noticeDao.insert(noticeEntity);
                }else {
                    System.out.println("数据已存在");
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("抛出异常");
            }
        }

        return "添加数据成功";
    }
}